﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAL;
using BUS;

namespace WEBSITE_BANDEAL.Admin
{
    public partial class SanPham_Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Quản lý sản phẩm";
            if (!IsPostBack)
                LoadData();

        }
        List<LOAI_SANPHAM> categoryWithParents = new List<LOAI_SANPHAM>();
        protected void load_categories(List<LOAI_SANPHAM> list, int ma, string kytu)
        {
            foreach (LOAI_SANPHAM cat in list)
            {
                if (ma == cat.Maloaicha)
                {
                    cat.Tenloaisp = "|" + kytu + cat.Tenloaisp;
                    categoryWithParents.Add(cat);
                    load_categories(list, cat.Maloai, kytu + kytu); //load that parent
                }
            }
        }
        //LOAD DU LIEU LEN DROPDOWNLIST
        private void LoadData()
        {

            //load Loai San Pham
            Loaisanphamcollection list = LOAI_SAN_PHAMBUS.GetAll();
            load_categories(list, 0, "-");
            MA_LOAI_SP.DataSource = categoryWithParents;
            MA_LOAI_SP.DataTextField = "Tenloaisp";
            MA_LOAI_SP.DataValueField = "Maloai";
            MA_LOAI_SP.DataBind();
            MA_LOAI_SP.Items.Insert(0, new ListItem("----NO PARENT---", "0"));
            //Load Tinh Thanh
            TINH_TP.DataSource = TINH_TPBUS.GetAll();
            TINH_TP.DataTextField = "Ten_tp";
            TINH_TP.DataValueField = tinh_tpColumns.Ma_tinh.ToString();
            TINH_TP.DataBind();

            //Chon thoi gian bat dau la ngay hien tai
            
        }


        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            SAN_PHAM sp = new SAN_PHAM();
            sp.Tensp = Ten.Value;
            sp.Maloaisp = int.Parse(MA_LOAI_SP.SelectedValue);


            sp.Tgbatdau = DateTime.Parse(TG_BAT_DAU.Text);
            sp.Tgketthuc = DateTime.Parse(TG_KETTHUC.Text);
            sp.Soluong = int.Parse(SO_LUONG.Value);
            sp.Soluongdaban = 0;
            sp.Matinh = int.Parse(TINH_TP.SelectedValue);
            sp.Dkapdung = DIEU_KIEN_AP_DUNG.Text;
            sp.Diemnoibat = DIEM_NOI_BAT.Text;
            sp.Giagoc = int.Parse(GIA_GOC.Value);
            sp.Giaban = int.Parse(GIA_BAN.Value);
            sp.Trangthai = 1;

            sp.Hinhdaidien = HinhAnh.Text;

            SAN_PHAMBUS.insert(sp);
            Response.Redirect("SanPham_List.aspx");
        }
    }
}